﻿using DrawIO45.Models.DrawIOs;
using DrawIO45.Models.Logic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DrawIO45.Draws.Business
{
    /// <summary>
    /// 画出来实现的接口
    /// </summary>
    public class DrawImplementedInterface : IDrawOnRoot
    {
        DrawImplementedInterfaceOption Option;

        public DrawImplementedInterface()
        {
            Option = new DrawImplementedInterfaceOption();
        }

        public DrawImplementedInterface(DrawImplementedInterfaceOption option)
        {
            Option = option;
        }

        public void DrawOnRoot(Root root, List<DILocationInfo<LClassInfo>> classInfos)
        {
            var types = classInfos.Select(n => n.Object.Source).ToArray();
            // 画实现接口关系线
            foreach (var itemType in types)
            {
                if (itemType.IsInterface)
                    continue;

                var interfaces = itemType.GetInterfaces();
                if (interfaces?.Any() != true)
                    continue;

                foreach (var itemInterface in interfaces)
                {
                    var currentLInfo = classInfos.FirstOrDefault(n => n.Object.Source == itemType);
                    if (currentLInfo == null)
                        continue;

                    if (DrawHelper.CanFindCellByLClass(root, currentLInfo.Object) != true)
                        continue;

                    if (Option.IgnoreWhenParentImplementedInterface)
                    {
                        if (itemType.BaseType != null)
                        {
                            // 找到父类
                            var parentType = types.FirstOrDefault(n => n == itemType.BaseType);
                            if (parentType != null)
                            {
                                if (parentType.GetInterfaces().Contains(itemInterface))
                                    continue;
                            }
                        }
                    }

                    var parentLInfo = classInfos.FirstOrDefault(n => n.Object.Source == itemInterface);
                    if (parentLInfo == null)
                        continue;

                    if (DrawHelper.CanFindCellByLClass(root, parentLInfo.Object) != true)
                        continue;

                    root.MxCells.Add(new MxCell
                    {
                        Id = Guid.NewGuid().ToString(),
                        Parent = "1",
                        Style = "endArrow=block;dashed=1;endFill=0;endSize=12;html=1;rounded=0;",
                        Edge = "1",
                        Source = currentLInfo.Object.Id,
                        Target = parentLInfo.Object.Id,
                        Value = "Impl",
                        MxGeometries = new List<MxGeometry>
                        {
                            new MxGeometry
                            {
                                Width = "160",
                                Relative = "1",
                                As = "geometry",
                            },
                        },
                    });
                }
            }
        }
    }

    /// <summary>
    /// 接口选项
    /// </summary>
    public class DrawImplementedInterfaceOption
    {
        /// <summary>
        /// 当父类实现了这个接口的时候，忽略这条线
        /// </summary>
        public bool IgnoreWhenParentImplementedInterface { get; set; }
    }
}
